package com.vincent.common.utils;

import org.apache.log4j.Logger;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Vincent
 * @version 1.0
 * @description 对象转换工具类
 */
public class O2OUtils {

    private static final Logger logger = Logger.getLogger(O2OUtils.class);

    /**
     * 根据对象转换为map
     *
     * @param t
     * @return
     */
    public static Map<String, Object> getMapFromObject(Object t) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            Class<?> aClass = t.getClass();
            Field[] fields = aClass.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                Object value = field.get(t);
                map.put(name, value);
            }
            return map;
        } catch (IllegalAccessException e) {
            logger.error(e);
        }

        return null;
    }

}
